﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using SP.SPProject.Core.Support;
using SP.SPProjects.WebParts.Model;
using System.Linq;

namespace SP.SPProjects.WebParts.GanntView
{
    public partial class GanntViewUserControl : UserControl
    {
        #region Properties

        public string TaskList { get; set; }

        #endregion

        #region Form Events

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnInit(EventArgs e)
        {
            try
            {

                base.OnInit(e);

                if (!ListHelper.ListExistsCurrentWeb(TaskList))
                    throw new Exception("List " + TaskList + " does not exists");

                if (!ListHelper.ListContainsContentType(Constants.TaskContentType, TaskList))
                    throw new Exception("List " + TaskList + " is not a task list");

                this.configureUI();
                this.Controls.Add(getGanntView());
            
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        #endregion

        #region Private Methods

        private Panel getGanntView()
        {
            SPList objSPList = SPContext.Current.Web.Lists[TaskList];
            SPView objSPView = objSPList.DefaultView;
            Gantt g = new Gantt("gantt", objSPList, objSPView, "Title", "StartDate", "TaskDueDate", "PercentComplete");
            var userTask = ListHelper.GetItemsByCurrentUser(TaskList,"AssignedTo");
            if (userTask.Count > 0)
            {
                g.DataSource = userTask.GetDataTable();
                pContainer.Controls.Add(g);
            }
            return pContainer;
        }

        private void configureUI()
        {
            pContainer.Height = 400;
        }

        #endregion
    }
}
